package com.szc.demo01.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szc.demo01.dao.AttenceMapper;
import com.szc.demo01.pojo.Attence;
import com.szc.demo01.service.AttenceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

import java.util.*;

@Slf4j
@Service
public class AttenceServiceImpl extends ServiceImpl<AttenceMapper, Attence> implements AttenceService {

   @Autowired
   private AttenceMapper attenceMapper;

    //上班的时间是8点
    private Integer is_latter_time=8;

    //下班时间是下午5点
    private Integer is_leave_time=17;
    @Override
    /**
     * return 是一个已经封装过的数据
     */
    public List<Attence> ATTENCE_LIST(int year,int month,long e_id) {
        List<Attence> listByMonth = attenceMapper.getListByMonth(month+1,e_id);
        return DataFormat(year,month,listByMonth);
    }

    // 将以月查询出来的数据做一个类似日期的封装;
    /**
     * GregorianCalendar 月份【0-11】
     * @param year
     * @param month
     * @return
     */
    public int getIntent(int year,int month){
        GregorianCalendar d = new GregorianCalendar();
        //设置GregorianCalendar对象年月日
        d.set(Calendar.MONTH,month);
        d.set(Calendar.YEAR,year);
        //当月第一天
        d.set(Calendar.DAY_OF_MONTH, 1);
        //当月第一天是星期几
        int weekDay=d.get(Calendar.DAY_OF_WEEK);
        int firstDayOfWeek = d.getFirstDayOfWeek();
        int intent = 0;
        while (weekDay != firstDayOfWeek) {
            intent++;
            //日子减一，一直减到当日是一周的第一天，得到缩进
            d.add(Calendar.DAY_OF_MONTH, -1);
            weekDay = d.get(Calendar.DAY_OF_WEEK);
        }
        return intent;
    }


    /**
     * 没有打卡的天数默认为空
     * @param list
     * @return
     */
    public List<Attence> DataFormat(int year,int month,List<Attence> list){
        Attence []attences=new Attence[42];
        Integer space=getIntent(year,month);

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd");
        //将天数作为对应的atteces数组的下标
        //数组初始化默认全为空；


        for(Attence attence:list){
           //如果那一天打卡，则attences数组的对应下标 对应该条数据库查询出来的数据
           Integer  day=Integer.parseInt( simpleDateFormat.format(attence.getWorkDate()));
           attences[day]=attence;
        }
        return Arrays.asList(attences);
    }

    /**
     * 下班有三种状态，上班只有两种
     * 0代表正常下班打卡
     * 1代表 没有打卡就下班打卡
     * 2代表 没有到下班时间
     * 3代表 系统出错
     * @param e_id
     * @return
     */
    @Override
    public int clock_out(Long e_id) {
        Date date=new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        String[] s = format.split(" ");

        Integer hours=Integer.parseInt(s[1].substring(0,s[1].indexOf(":")));

        Attence attence = getAttenceByEidAndWorkDate(e_id,java.sql.Date.valueOf(s[0]));
        //如果没有记录 就跳转打卡
        if(attence==null) {
            return 1;
        }
        if(hours<is_leave_time){
            return 2;
        }
        Long start= null;
        start = attence.getWorkStart().getTime();

        Long end=date.getTime();
        Long work_time=end-start;
        int i = attenceMapper.updateAttenceByEidAndWorkDate(format,work_time,e_id,s[0]);
        return i>0?0:3;
    }

    @Override
    public Attence getAttenceByEidAndWorkDate(Long e_id, Date work_date) {
        return attenceMapper.getAttenceByEidAndWorkDate(e_id,work_date);
    }

    /**
     * 上班打卡
     * 0代表正常上班打卡
     * 1代表 已经打过卡
     * 2代表 系统出错
     * @param e_id
     * @return
     */
    @Override
    public int clock_in(Long e_id) {
        Date date=new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        String []s=format.split(" ");
        //打卡前先查询一下，如果已打卡 返回true
        if(getAttenceByEidAndWorkDate(e_id,java.sql.Date.valueOf(s[0]))!=null) {
            return 1;
        }
        //获取小时
        Integer hours=Integer.parseInt(s[1].substring(0,s[1].indexOf(":")));
        Attence attence= new Attence();

        attence.setWorkDate(date);
        attence.setEId(e_id);

        attence.setWorkStart(date);
        attence.setWorkTime(0);
        attence.setWorkEnd(date);
       // ( e_id, work_date, work_start,  work_time, is_later, is_leave )  VALUES  ( ?, ?, ?,  ?, ?, ? )
        //0表示否代表没有迟到
        if(hours>is_latter_time){
            attence.setIsLater(1);
            attence.setMark("迟到");
        }else {
            attence.setIsLater(0);
        }
        int insert = attenceMapper.insertAttence(attence);
        return insert>0?0:2;
    }

    @Override
    public List<Attence> getListForBoss(Long e_id, Integer month, String name) {
        return  attenceMapper.getListForBoss(e_id,month,name);
    }

    //按照某一天来查
    @Override
    public List<Attence> getListForBossByDay(String work_date) {
        return  attenceMapper.getListForBossByDay(work_date);
    }
}
